package com.ocean.web.controller.product;

import java.util.List;

import com.ocean.resources.domain.OResources;
import com.ocean.resources.service.IOResourcesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import com.ocean.common.annotation.Log;
import com.ocean.common.enums.BusinessType;
import com.ocean.metadata.domain.OMetadata;
import com.ocean.metadata.service.IOMetadataService;
import com.ocean.common.core.controller.BaseController;
import com.ocean.common.core.domain.AjaxResult;
import com.ocean.common.utils.poi.ExcelUtil;
import com.ocean.common.core.page.TableDataInfo;

/**
 * 元数据模板Controller
 *
 * @author ocean
 * @date 2023-01-29
 */
@Controller
@RequestMapping("/metadata/metadata")
public class OMetadataController extends BaseController
{
    private String prefix = "metadata/metadata";

    @Autowired
    private IOMetadataService oMetadataService;
    @Autowired
    private IOResourcesService resourcesService;

    @GetMapping()
    public String metadata(ModelMap mmap, @RequestParam String id,@RequestParam String resourcesId)
    {
        OMetadata metadata = new OMetadata();metadata.setProductId(id);
        List<OMetadata> list = oMetadataService.selectOMetadataList(metadata);
        if (!CollectionUtils.isEmpty(list)) metadata = list.get(0);
        OResources resources = resourcesService.selectOResourcesById(Long.parseLong(resourcesId));
        mmap.addAttribute("resources",resources);
        mmap.addAttribute("metadata",metadata);
        return "product/product/metadata";
    }

    /**
     * 查询元数据模板列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OMetadata oMetadata)
    {
        startPage();
        List<OMetadata> list = oMetadataService.selectOMetadataList(oMetadata);
        return getDataTable(list);
    }

    /**
     * 导出元数据模板列表
     */
    @Log(title = "元数据模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OMetadata oMetadata)
    {
        List<OMetadata> list = oMetadataService.selectOMetadataList(oMetadata);
        ExcelUtil<OMetadata> util = new ExcelUtil<OMetadata>(OMetadata.class);
        return util.exportExcel(list, "元数据模板数据");
    }

    /**
     * 新增元数据模板
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存元数据模板
     */
    @Log(title = "元数据模板", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody OMetadata oMetadata)
    {
        return toAjax(oMetadataService.insertOMetadata(oMetadata));
    }

    /**
     * 修改元数据模板
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        OMetadata oMetadata = oMetadataService.selectOMetadataById(id);
        mmap.put("oMetadata", oMetadata);
        return prefix + "/edit";
    }

    /**
     * 修改保存元数据模板
     */
    @Log(title = "元数据模板", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OMetadata oMetadata)
    {
        return toAjax(oMetadataService.updateOMetadata(oMetadata));
    }

    /**
     * 删除元数据模板
     */
    @Log(title = "元数据模板", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(oMetadataService.deleteOMetadataByIds(ids));
    }
}
